package com.yzk.auction.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.net.URLDecoder;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * Author: 园中葵小组成员
 * Description:JDBC工具类(获取DataSource对象，Connection对象)
 */
public class JDBCUtils {
    private static DataSource dataSource;
    /**
     * 加载配置文件，获取DataSource对象
     */
    static {
        Properties properties = new Properties();
        String propertyFile = JDBCUtils.class.getResource("/druid-config.properties").getPath();
        try{
            propertyFile = new URLDecoder().decode(propertyFile,"UTF-8");
            properties.load(new FileInputStream(propertyFile));
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 获取连接池对象
     * @return DataSource对象
     */
    public static DataSource getDataSource(){
        return dataSource;
    }

    /**
     * 获取连接Connection对象
     * @return Connection对象
     * @throws SQLException
     */
    public static Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }
}
